/**
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: 联动优势科技有限公司</p>
 * <p>Apr 24, 20137:05:53 PM</p>
 * @author Ding Zhe
 * @version 1.0
 */
package com.umpay.privi.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.umpay.privi.common.BaseException;
import com.umpay.privi.common.XmlData;

/** 
 * desc:
 * <p>创建人：Ding Zhe 创建日期：Apr 24, 2013 </p>
 * @version V1.0  
 */
public class DateUtil{
	public static String formatDay8Str = "yyyyMMdd";
	public static String formatDay10Str = "yyyy-MM-dd";
	public static String formatDay11Str = "yyyy年MM月dd日";
	public static String formatDay12Str = "yyMMddHHmmss";//YYMMDDHHMMSS
	public static String formatTime14Str = "yyyyMMdd HHmmss";
	public static String formatTime15Str = "yyyy-MM-dd HH:mm";
	public static String formatTime16Str = "yyyyMMdd HH:mm:ss";
	public static String formatTime18Str = "yyyy-MM-dd HH:mm:ss";
	public static String formatTime24Str = "yyyy-MM-dd HH:mm:ss.SSSSSS";
	public static String formatTime22Str = "yyyyMMdd HH:mm:ss.SSSSSS";
	public static String formatTime6Str = "HHmmss";
	public static String formatTime8Str = "HH:mm:ss";
	public static String formatTime10Str = "MMddHHmmss";
	public static String formatTimeHHmmStr = "HHmm";
	public static String date(String fmt) {
		return new SimpleDateFormat(fmt).format(new Date());
	}

	public static String date(String fmt, long t) {
		return new SimpleDateFormat(fmt).format(new Date(t));
	}

	public static String date8() {
		return new SimpleDateFormat(formatDay8Str).format(new Date());
	}

	public static String date8(Date date) {
		return new SimpleDateFormat(formatDay8Str).format(date);
	}
	public static String date8(Timestamp date) {
		return new SimpleDateFormat(formatDay8Str).format(date);
	}
	public static String date12() {
		return new SimpleDateFormat(formatDay12Str).format(new Date());
	}

	public static String time6() {
		return new SimpleDateFormat(formatTime6Str).format(new Date());
	}

	public static String time6(Date date) {
		return new SimpleDateFormat(formatTime6Str).format(date);
	}
	public static String time6(Timestamp date) {
		return new SimpleDateFormat(formatTime6Str).format(date);
	}

	public static String datetime14() {
		return new SimpleDateFormat(formatTime14Str).format(new Date());
	}

	public static String datetime14(Date date) {
		return new SimpleDateFormat(formatTime14Str).format(date);
	}

	public static String datetime14(long t) {
		return new SimpleDateFormat(formatTime14Str).format(new Date(t));
	}
	
	public static Timestamp formatString2Timestamp(String date,String format) throws ParseException {
		if(null==date||("").equals(date))return null;
		if(null==format||("").equals(format))return null;
		return new Timestamp(formatStringToDate(date,format).getTime());
	}
	public static Date formatStringToDate(String date,String format) throws ParseException {
		if(null==date||("").equals(date))return null;
		if(null==format||("").equals(format))return null;
		SimpleDateFormat format2 = new SimpleDateFormat(format);
		Date newDate = format2.parse(date);
		return newDate;
	}
	/**
	 * 20071231转换为2007-12-31
	 * @param date
	 * @return
	 */
	public static String date8(String date){
		if(null==date)return null;
		date = date.trim();
		if(date.length()!=8)return null;
		else return date.substring(0,4)+"-"+date.substring(4,6)+"-"+date.substring(6,8);
	}
	
	/**
	 * 格式化日期
	 * @param orderInfoList
	 * @
	 */
	public static String formatDate(Date date)  {
		if(date == null) {
			return null;
		}
		SimpleDateFormat newformat = new SimpleDateFormat("yyyy-MM-dd"); //需要展现的时间格式
		return newformat.format(date);
		
	}
	
	public static String formatDate(Date date, String format) {
		if(date == null) {
			return null;
		}
		SimpleDateFormat newformat = new SimpleDateFormat(format); //需要展现的时间格式
		return newformat.format(date);
	}
	
	public static Date addMonth(Date date, int month) {		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);
		return calendar.getTime();
	}
	
	public static Date addDay(Date date, int day) {		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, day);
		return calendar.getTime();
	}
	
	public static Date addSecond(Date date, int second) {		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.SECOND, second);
		return calendar.getTime();
	}
	
	public static Date addYear(Date date, int year) {		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, year);
		return calendar.getTime();
	}
	
	public static Date getPreDate(Date date, int month) throws ParseException {	
		Date tempDate = addMonth(date,month);
		SimpleDateFormat format = new SimpleDateFormat(formatDay8Str);
		String preDayStr = format.format(tempDate);
		return formatStringToDate(preDayStr, formatDay8Str);
	}
	
	public static String getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR) + "";
	}
	
	public static String conversFormat(String time, String srcFormat, String targetFormat) throws ParseException  {
		Date srcDate = formatStringToDate(time.trim(), srcFormat);
		String result = formatDate(srcDate, targetFormat); //交易日期 
		return result;
	}
	
	 /**
	 * 判断是否是周末
	 * @param date 表示日期的字符串，比如20100424
	 * @param pattern 描述日期和时间格式的模式，比如yyyyMMdd
	 * @return
	 * @throws ParseException 
	 * @throws ParseException
	 */
    public static boolean isWeekend(String date,String pattern) throws ParseException  {
    	SimpleDateFormat df = new SimpleDateFormat(pattern);
	    Date time = df.parse(date);
	    Calendar cal = Calendar.getInstance();
	    cal.setTime(time);
		
	    int day = cal.get(Calendar.DAY_OF_WEEK);
	    
	    if (day == Calendar.SATURDAY || day == Calendar.SUNDAY){
	    	return true;
	    }
	    else{
	    	return false;
	    }
    }
    
    public static int getWeek(Date date)  {
    	 Calendar cal = Calendar.getInstance();
 	    cal.setTime(date);
 		
 	    int day = cal.get(Calendar.DAY_OF_WEEK);
 	    return day;
	}
    
    
    public static boolean isWeekend(Date date)  {
	    Calendar cal = Calendar.getInstance();
	    cal.setTime(date);
		
	    int day = cal.get(Calendar.DAY_OF_WEEK);
	    
	    if (day == Calendar.SATURDAY || day == Calendar.SUNDAY){
	    	return true;
	    }
	    else {
	    	return false;
	    }
    }
	
	/**
	 * 获取距当前日期n+1个月的年信息，如当前日期为2009-02-03，n=3，则结果为{2008,2009}
	 * 
	 * @param monthCount
	 * @return
	 */
	public static String[] getYearList(int monthCount) {
		if (monthCount > 0)
			monthCount = 0 - monthCount;// 保证是向前推
		Calendar cal = Calendar.getInstance();
		int year1 = cal.get(Calendar.YEAR);
		cal.add(Calendar.MONTH, monthCount);// 实际推了n+1月
		int year2 = cal.get(Calendar.YEAR);
		String[] retVal = new String[year1 - year2 + 1];
		for (int i = 0; year1 >= year2; year1--, i++) {
			retVal[i] = year1 + "";
		}
		return retVal;
	}
	
	/**
	 * 将日期格式的字符串格式化成指定的日期格式
	 * 
	 * @param dateStr
	 *            源日期字符串
	 * @param formatStr1
	 *            源日期字符串日期格式
	 * @param formatStr2
	 *            新日期字符串日期格式
	 */
	public static String formatDateStringToString(String dateStr,
			String formatStr1, String formatStr2) {
		try {
			if (null == dateStr)
				return null;
			SimpleDateFormat format = new SimpleDateFormat(formatStr1);
			Date date = format.parse(dateStr);
			return new SimpleDateFormat(formatStr2).format(date);
		} catch (Exception ex) {
			return null;
		}

	}
	/**
	 * 将日期转换为新的格式
	 * @param dateStr
	 * @param formatStr1
	 * @param intervalDate
	 * @param formatStr2
	 * @return
	 * @see:
	 */
	public static String formatDateStringToString(String dateStr, String formatStr1, int intervalDate, String formatStr2) {
		try {
			if (null == dateStr)
				return null;
			SimpleDateFormat format = new SimpleDateFormat(formatStr1);
			Date date = format.parse(dateStr);
			Date newDate = addDay(date, intervalDate);
			return new SimpleDateFormat(formatStr2).format(newDate);
		} catch (Exception ex) {
			return null;
		}
		
	}
	
	/**
	 * 格式化时间戳为字符串
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String formatTimestamp2String(Timestamp date, String format) {
		if (null == date)
			return null;
		if (null == format || ("").equals(format))
			return null;
		return new SimpleDateFormat(format).format(date);
	}
	/**
	 * 获取查询日期的区间
	 * 例如：endDate是20110713，interval是3，表示三个月前，则返回 stateDate= 20110413 endDate=20110713的map
	 * @param endDate 结束日期
	 * @param interval 时间间隔
	 * @return
	 * @
	 */
	public static Map<String, String> getQryDateInterval(Date endDate, int intervalMonth, String format)  {
		int tempInt = 0 - intervalMonth;
		Date preDate = addMonth(endDate, tempInt);
		String stateDateStr = formatDate(preDate, format);
		String endDateStr = formatDate(endDate, format);
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put(XmlData.START_DATE, stateDateStr);
		resultMap.put(XmlData.END_DATE, endDateStr);
		return resultMap;
	}

	
	public static void validateDateInterval(String startDateStr, String endDateStr, int intervalMonth, String format) throws BaseException {
		startDateStr = StringUtil.trim(startDateStr);
		endDateStr = StringUtil.trim(endDateStr);
		format = StringUtil.trim(format);
		if ("".equals(startDateStr)) {
			//如果开始或结束日期为空，返回-1;
			throw new BaseException("开始日期不能为空");
		}
		
		if ("".equals(endDateStr)) {
			//如果开始或结束日期为空，返回-1;
			throw new BaseException("结束日期不能为空");
		}
		
		Date startDate = null;
		Date endDate = null;
		
		try {
			startDate = formatStringToDate(startDateStr, format);
		} catch (ParseException e1) {
			throw new BaseException("开始日期格式不正确");
		}
		
		try {
			endDate = formatStringToDate(endDateStr, format);
		} catch (ParseException e1) {
			throw new BaseException("结束日期格式不正确");
		}
		
		if (startDate.after(endDate)) {
			//如果结束日期在开始日期之前，返回2
			throw new BaseException("结束日期不能在开始日期之前");
		}
		
		Date nowDate = new Date();
		Date preDate = addMonth(nowDate, -intervalMonth);
		
		if (endDate.after(nowDate)) {
			//结束日期不能超过当前日期
			throw new BaseException("结束日期不能超过当前日期");
		}
		
		if (startDate.before(preDate)) {
			//开始日期不在日期范围之内
			throw new BaseException("开始日期不在日期范围之内");
		}
	}
	
	/**
	 * 
	 * description: 判断当前时间是否为19点以后
	 * @param endDate
	 * @param amount
	 * @return
	 */
	public static boolean nowIsNight() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int i =cal.get(Calendar.HOUR_OF_DAY);
		return i >18;
	}
	public static String datetime10() {
		return new SimpleDateFormat(formatTime10Str).format(new Date());
	}
}
